package com.Rsonly.spring.cloud.weather.job;

import com.Rsonly.spring.cloud.weather.service.CityDataService;
import com.Rsonly.spring.cloud.weather.service.WeatherDataService;
import com.Rsonly.spring.cloud.weather.service.WeatherDataServiceImpl;
import com.Rsonly.spring.cloud.weather.vo.City;
import org.quartz.JobExecutionException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

/**
 * @author 34234
 */
public class WeatherDataSyncJob extends QuartzJobBean {

    private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(WeatherDataServiceImpl.class);

    @Autowired
    private CityDataService cityDataService;

    @Autowired
    private WeatherDataService weatherDataService;

    /**
     * Execute the actual job. The job data map will already have been
     * applied as bean property values by execute. The contract is
     * exactly the same as for the standard Quartz execute method.
     *
     * @param context
     * @see #execute
     */
    @Override
    protected void executeInternal(org.quartz.JobExecutionContext context) throws JobExecutionException {
        LOGGER.info("Weather Data Sync Job Start!");

        //获取城市id列表
        List<City> cityList = null;

        try {
            cityList = cityDataService.listCity();
        } catch (Exception e) {
            LOGGER.error("Exception:" + e.getMessage());
        }

        //遍历城市id获取天气
        for (City city : cityList) {
            String cityId = city.getCityId();
            LOGGER.info("Weather Data Sync Job,cityId:" + cityId);

            weatherDataService.syncDataByCityId(cityId);
        }

        LOGGER.info("Weather Data Sync Job end!");
    }
}
